package com.boot.service.impl;

import com.boot.entity.Ad;
import com.boot.entity.Push;
import com.boot.entity.User;
import com.boot.mapper.AdDao;
import com.boot.mapper.PushDao;
import com.boot.mapper.UserDao;
import com.boot.service.PushService;
import com.boot.util.AdResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;

@Service
public class PushServiceImpl implements PushService {

	@Resource
	private PushDao pushDao;
	@Resource
	private UserDao userDao;
	@Resource
	private AdDao adDao;
	
	//增加事务
	@Transactional
	@Override
	public AdResult acceptPushAd(String ad_id, String user_accept_id) {
		AdResult result=new AdResult();
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("ad_id", ad_id);
		map.put("user_accept_id", user_accept_id);
		int afectedLine=pushDao.updatePushAccept(map);
		Ad ad=adDao.getAdById(ad_id);
		if(afectedLine>0){
			if(ad!=null){
				result.setData(ad);
			}
			result.setMsg("用户确认查看");
			result.setStatus(1);
			//将单条广告的费用划拨给广告观看者
			Push push=pushDao.getAdPrice(map);
			if(push!=null){
				User user=new User();
				BigDecimal bigDecimal=new BigDecimal(String.valueOf(push.getPrice()));
				user.setBalance(bigDecimal);
				user.setUser_id(user_accept_id);
				userDao.updateUserBalance(user);
			}
		}else {
			result.setMsg("确认失败");
			result.setStatus(0);
		}
		return result;
	}

}
